package com.linjian.togetherbirthday.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.contant.HttpContant;
import com.linjian.togetherbirthday.utils.HttpUtils;
import com.linjian.togetherbirthday.utils.NoticeEvent;
import com.linjian.togetherbirthday.utils.SharedPreferencesUtil;
import com.sdsmdg.tastytoast.TastyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangePasswrodFragment extends BaseFragment implements View.OnClickListener {

    private EditText newPwdEt,okPwdEt;
    private TextView confirmPwdTv;
    private ImageView backIv;
    private TextView titleTv,rightTv;

    public static ChangePasswrodFragment newInstance() {
        ChangePasswrodFragment changePasswrodFragment = new ChangePasswrodFragment();
        Bundle bundle = new Bundle();
        changePasswrodFragment.setArguments(bundle);
        return changePasswrodFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int setView() {
        return R.layout.fragment_change_passwrod;
    }

    @Override
    protected void init(View view) {
        backIv = view.findViewById(R.id.layout_head_back_iv);
        titleTv = view.findViewById(R.id.layout_head_title_tv);
        rightTv = view.findViewById(R.id.layout_head_right_tv);
        newPwdEt = view.findViewById(R.id.change_passwrod_one_et);
        okPwdEt = view.findViewById(R.id.change_passwrod_two_et);
        confirmPwdTv = view.findViewById(R.id.change_passwrod_tv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        titleTv.setText("修改密码");
        rightTv.setVisibility(View.GONE);
        setListener();
    }

    private void setListener() {
        backIv.setOnClickListener(this);
        confirmPwdTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_head_back_iv:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
            case R.id.change_passwrod_tv:
                if (TextUtils.isEmpty(newPwdEt.getText())||TextUtils.isEmpty(okPwdEt.getText())){
                    TastyToast.makeText(getActivity(),"选项不能为空",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else if (!newPwdEt.getText().toString().endsWith(okPwdEt.getText().toString())){
                    TastyToast.makeText(getActivity(),"密码不一致",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else {
                    httpChangePasswrod(newPwdEt.getText().toString(),okPwdEt.getText().toString());
                }
                break;
        }
    }

    /**
     * 修改密码接口
     */
    private void httpChangePasswrod(String password,String pwd){
        Map<String,Object> map = new HashMap<>();
        map.put("password",password);
        map.put("pwd",pwd);
        HttpUtils.PutCookie(HttpContant.URL + HttpContant.ChangePwd
                , "SESSION=" + String.valueOf(SharedPreferencesUtil.get(getActivity(), "Cookie", ""))
                , map
                , new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        EventBus.getDefault().post(new NoticeEvent("requestFail"));
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        EventBus.getDefault().post(new NoticeEvent("changePasswrod",response.body().string()));
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(NoticeEvent event){
        switch (event.getCls()){
            case "requestFail":
                TastyToast.makeText(getActivity(),"请求失败",TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                break;
            case "changePasswrod":
                try {
                    JSONObject object = new JSONObject(event.getData().toString());
                    if (object.getInt("code") == 200){
                        TastyToast.makeText(getActivity(),object.getString("data"),TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
                        getActivity().getSupportFragmentManager().popBackStack();
                    }else {
                        TastyToast.makeText(getActivity(),object.getString("msg"),TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
